package com.wimcorp.magic.dataloader.costs;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.wimcorp.magic.data.CostData;
import com.wimcorp.magic.input.CostInput;

public class ManaCostWidget extends CostWidget {

	private TextBox manaCost;

	public ManaCostWidget(CostInput cost) {
		super(cost);

		manaCost = new TextBox();
		createColorPanel("Manacost", manaCost);
	}

	private void createColorPanel(String label, TextBox textBox) {
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(new Label(label));
		panel.add(textBox);
		getConfigurationPanel().add(panel);
	}

	public void load(CostData cost) {
		JSONObject config = (JSONObject) JSONParser.parseLenient(cost.getConfig());
		manaCost.setText(toString(config.get("manaCost")));
	}

	public String getManaCost() {
		return manaCost.getText();
	}
	
	public String getConfig() {
		JSONObject config = new JSONObject();
		config.put("manaCost", new JSONString(manaCost.getText()));
		return config.toString();
	}

}
